package com.gome.han.bigdata.spark.core.rdd.operation.action

import org.apache.spark.{SparkConf, SparkContext}

/**
 * @author Hanpeng
 * @date 2021/1/14 20:59
 * @description:
 */
object AggregateOperation {
  def main(args: Array[String]): Unit = {
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("Operator")
    val sc = new SparkContext(sparkConf)

    val rdd = sc.makeRDD(List(1,2,3,4),2)

    // TODO - 行动算子

    //10 + 13 + 17 = 40
    // aggregateByKey : 初始值只会参与分区内计算
    // aggregate : 初始值会参与分区内计算,并且和参与分区间计算
    //val result = rdd.aggregate(10)(_+_, _+_)
    //分区内和分区间的计算规则相同 fold是aggregate的简写版本
    val result = rdd.fold(10)(_+_)

    println(result)

    sc.stop()
  }
}
